var searchPage = function () {
};
$(function () {
    searchPage = function () {
        this.init();
    };

    searchPage.prototype = {
        // 初始化
        init: function () {
            this.paintpage();
            this.loadhotgoods();
            this.bindEvent();
        },

        // 加载商品列表数据
        paintpage: function () {
            var html = [];
            html.push("<span id='searchpage' style='display:none'>");

            html.push("<div class='header' style='z-index:11'>");
            html.push("<span class='headleft' id='btncloseseahpage' style='width:15%'><span class='back'><i class='iconfont icon-jiantou'></i></span></span>");
            html.push("<span class='tbbiaoti' style='width:70%'><div class='search' id='searchinput'>");
            html.push("<p><input type='text' id='searchtext' placeholder='搜索您喜欢的店铺/商品' class='sskuang'></p>");
            html.push("</div></span>");
            html.push("<span class='headright' style='width:15%' id='btnsearch'><span class='xiaox qxan'>搜索</span></span></div>");

            html.push("<div class='main bgbai zitidx' style='z-index:10'><div class='shaixuan mbbottom'>");
            html.push("<p class='shaixbt huizi1'>热门搜索</p>");
            html.push("<ul class='shaixnr heizi' id='hotwordlistsearch'>");
            html.push("");
            html.push("</ul>");
            html.push("</div>");
            html.push("<div class='shaixuan'>");
            html.push("<p class='shaixbt huizi1'>历史记录</p>");
            html.push("<div id='searchhistorylist'></div>");
            html.push("<p class='lsjlan'>历史记录</p>");
            html.push("</div></span>");

            $("body").append(html.join(""));
        },

        // 热门商品
        loadhotgoods: function () {
            $.ajax({
                type: "GET",
                url: CONFIG_MANAGER.RequstUrl + "v1/goods/search/tag/18"
            }).done(function (res) {
                if (res && res.length > 0) {
                    var html = [];
                    $.each(res, function (i, tagitem) {
                        html.push("<li tip='" + tagitem.catagory_no + "'>" + tagitem.name + "</li>")
                    });
                    $("#hotwordlistsearch").html(html.join(""));
                }
            });
        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 搜索历史

            // 回退
            $("#btncloseseahpage").on("tap", function () {
                closeSearchPage();
            });

            // 搜索
            $("#btnsearch").on("tap", function () {
                closeSearchPage();
                var searchword = $("#searchtext").val();
                setSeachHistoryStorage(searchword);

                model.search(searchword);
            });

            // 热门搜索
            $("#hotwordlistsearch").on("tap", "li", function () {
                closeSearchPage();
                var searchword = $(this).attr("tip") + "|" + $(this).text();
                setSeachHistoryStorage(searchword);

                model.search(searchword);
            });

            // 历史纪录
            $("#searchhistorylist").on("tap", "p[name=historysearch]", function () {
                closeSearchPage();
                var searchword = $(this).text();

                if ($(this).attr("tip")) {
                    searchword = $(this).attr("tip") + "|" + $(this).text();
                }

                setSeachHistoryStorage(searchword);

                model.search(searchword);
            });

            // 删除历史纪录
            $("#searchhistorylist").on("tap", "p[name=delhistory]", function () {
                var searchtext = $(this).parent().find('p[name=historysearch]').text();
                var list = JSON.parse(localStorage.getItem(STORAGE_MANAGER.searchhistory));
                var itemindex = list.indexOf(searchtext);
                if (itemindex > -1) {
                    list.splice(itemindex, 1);
                }
                localStorage.setItem(STORAGE_MANAGER.searchhistory, JSON.stringify(list));

                $(this).parent().remove();
            });


            function closeSearchPage() {
                $("#goodspage").show();
                $("#searchpage").hide();
            }

            function setSeachHistoryStorage(searchtext) {
                searchtext = searchtext.trim();
                $("#searchtext").val(searchtext);
                $("#searchtext").blur();
                if (searchtext == "") {
                    return;
                }
                var searchhistory = getLocalStorage(STORAGE_MANAGER.searchhistory);
                var list = [];
                if (searchhistory) {
                    list = searchhistory;
                }
                var itemindex = list.indexOf(searchtext);
                if (itemindex > -1) {
                    list.splice(itemindex, 1);
                }
                list.unshift(searchtext);
                setLocalStorage(STORAGE_MANAGER.searchhistory, list);
            }
        },

        // 历史纪录
        loadSearchHistory: function () {
            var list = getLocalStorage(STORAGE_MANAGER.searchhistory);
            if (!list) {
                return;
            }
            if (list.length > 10) {
                list.splice(10);
            }
            var html = [];
            for (var i = 0; i < list.length; i++) {
                html.push("<div class='mbbottom lsjl'>");
                if (list[i].indexOf("|") > -1) {
                    var catory = list[i].split('|');
                    html.push("<p class='lsjlny floatl heizi' tip='" + catory[0] + "' name='historysearch'>" + catory[1] + "</p>");
                } else {
                    html.push("<p class='lsjlny floatl heizi' name='historysearch'>" + list[i] + "</p>");
                }
                html.push("<p class='chahao' name='delhistory'><img src='../../images/chahao.png' /></p>");
                html.push("</div>");
            }
            $("#searchhistorylist").html(html.join(""));
        },
        // 当前显示状态
        isDisplay: false,
        // 显示
        show: function () {
            this.isDisplay = true;
            $("#searchpage").show();
            // 加载数据
            this.loadSearchHistory();
        },
        // 隐藏
        hide: function () {
            this.isDisplay = false;
            $("#searchpage").hide();
        },

        // 选择条件后回掉函数
        search: function (searchword) {
            if (searchword.indexOf('|') > -1) {
                var catory = searchword.split('|');
                var redirecturl = "../goods/goodslist.html?catalog={0}&searchword={1}".format(catory[0], catory[1]);
                redirect(redirecturl);
            } else {
                redirect("../goods/goodslist.html?searchword=" + searchword);
            }
            this.hide();
        }
    };
});
